<script type="text/javascript">
    $(document).ready(function(){
        $("select[id$=country]").change(function(){
            var stateField = $(this).closest('form').find('select[id$=state]')

            if ($(this).val() == 'US' || $(this).val() == 'CA'){
                  stateField.removeAttr('disabled')
                  stateField.html(loadStates($(this).val()))
                  stateField.closest('p').fadeIn()
            } else {
                  stateField.attr('disabled',true)
                  stateField.html('')
                  stateField.closest('p').fadeOut()
            }

        });

        function loadStates(country) {
            var str = '';

            switch (country) {
                case 'US':
                    var states = <?php echo json_encode(RCMS_Object_QuickConfig_QuickConfig::$stateListUSA); ?> ;
                    break;
                case 'CA':
                    var states = <?php echo json_encode(RCMS_Object_QuickConfig_QuickConfig::$stateListCanada); ?> ;
                    break;
                default:
                    var states = {}
                    break;
            }
            for (var state in states){
                str += '<option value="'+state+'">'+states[state]+'</option>'
            }
            return str;
        }

        $('#form_quote').submit(function(event){
            event.preventDefault()

            var q = $(this);
            var id = '<?php echo $this->productId;?>' ;
            var email = trim($('#form-quote-email').val()) ;
            if (trim($('#form-quote-firstname').val())==''){
                $('#form-quote-firstname').css('border-color','red').focus() ;
                return false;
            } else {$('#form-quote-firstname').css('border-color','#7c7c7c')}
            if (email=='' || !email.match(/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})$/i)){
                $('#form-quote-email').css('border-color','red').focus() ;
                return false;
            } else {$('#form-quote-email').css('border-color','#7c7c7c')}

            var data = {
                          firstname: $('#form-quote-firstname').val()
                        , lastname: $('#form-quote-lastname').val()
                        , email: $('#form-quote-email').val()
                        , company: $('#form-quote-company').val()
                        , address1: $('#form-quote-address1').val()
                        , address2: $('#form-quote-address2').val()
                        , city: $('#form-quote-city').val()
                        , state: $('#form-quote-state').val() !== null ? $('#form-quote-state').val() : ''
                        , zip: $('#form-quote-zip').val()
                        , country: $('#form-quote-country').val()
                        , phone: $('#form-quote-phone').val()
                        , comments: $('#form-quote-comments').val()
                    }

            q.queue("ajax", function(next){$(this).append('<div id="statusmsg">{%Generating quote, please wait%}...');next()});
            q.queue("ajax", function(next){
                var options = []
                var optsSelects = $('#optholder-' + id).find('select');
                if(optsSelects.length) {
                    $.each(optsSelects, function() {
                    var optType = '';
                    var optVal  = '';
                    $(this).find('option:selected').each(function(){
                        optType = $(this).val();
                        optVal  = $(this).text();
                    })
                    options.push(optType + '=' + optVal);
                    });
                }
                var qty = $('#add-amount-'+id).val();
                if (!/^([0-9]{1,10})$/.test(qty)) {
                    qty = 1;
                }
                $.ajax({
                    async: false
                    , url: '<?php echo $this->siteUrl; ?>sys/backend_shopping/addtocart/'
                    , type: 'POST'
                    , data: ({id:id, opts:options.toString(), amount:qty})
                    , dataType: 'json'
                    , success: function() {}
                });
                next()
            });
            q.queue("ajax", function(next){
                $.ajax({
                    async: false,
                    url: '<?php echo $this->siteUrl; ?>sys/backend_shipping/calculate/',
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        shippingService : '<?php echo (!empty($shoppingConfig) && $shoppingConfig['shipping_type'] == 'external') ? 'external' : 'internal' ; ?>'
                        , firstname : data.firstname
                        , lastname : data.lastname
                        , company : data.company
                        , email : data.email
                        , phone : data.phone
                        , address1 : data.address1
                        , address2 : data.address2
                        , city : data.city
                        , state : data.state
                        , zip : data.zip
                        , country : data.country
                        
                    },
                    success: function(responce){
                        //console.log(responce);
                        next();
                    }
                });
            });
            q.queue("ajax", function(next){
                $.ajax({
                    url: '<?php echo $this->siteUrl; ?>sys/backend_shopping/quoteme/'
                    , type: 'POST'
                    , data: {
                          name: data.firstname+' '+data.lastname
                        , company: data.company
                        , email : data.email
                        , phone : data.phone
                        , address1 : data.address1
                        , address2 : data.address2
                        , city : data.city
                        , state : data.state
                        , zip : data.zip
                        , country : data.country
                        , comments : '{%Client sent following comment%}: \n\r'+$('#form-quote-comments').val()
                        , autoquote: true
                    }
                    , dataType: 'json'
                    , success: function(result){
                        $.post('<?php echo $this->siteUrl; ?>sys/backend_shopping/recount/',{toremove: id});
                        if (result.done == true){
							if (result.redirect != ''){
								$('#statusmsg').text('{%Done. Redirecting to quote preview%}...');
								window.location.href=result.redirect
							} else {
								$('#statusmsg').text('{%Done%}.').delay(3000).fadeOut();
							}
                        }
                    }
                });
                next()
            })

            q.dequeue("ajax")
        });

		<?php if (isset($this->location) && !empty($this->location) ) : ?>
			$('#form-quote-country').val('<?php echo $this->location['country'];?>').change();
			<?php if (!empty($this->location['state'])) : ?>
			$('#form-quote-state').val('<?php echo $this->location['state'];?>');
			<?php endif; ?>
		<?php endif; ?>
    });
</script>
<form action="javascript:;" name="form_quote" id="form_quote">
    <label>{%Request quote now%}:</label>
    <p><label>{%Firstname%}:<span>*</span></label><input type="text" name="form-quote-firstname" id="form-quote-firstname"></p>
    <p><label>{%Lastname%}:</label><input type="text" name="form-quote-lastname" id="form-quote-lastname"></p>
    <p><label>{%Company%}:</label><input type="text" name="form-quote-company" id="form-quote-company"></p>
    <p><label>{%E-mail%}:<span>*</span></label><input type="text" name="form-quote-email" id="form-quote-email"></p>
    <p><label>{%Phone%}:</label><input type="text" name="form-quote-phone" id="form-quote-phone"></p>
    <p><label>{%Address%} 1:</label><input type="text" name="form-quote-address1" id="form-quote-address1"></p>
    <p><label>{%Address%} 2:</label><input type="text" name="form-quote-address2" id="form-quote-address2"></p>
    <p><label>{%Country%}:</label>
       <?php echo $this->formSelect('form-quote-country', null, null, RCMS_Object_QuickConfig_QuickConfig::$worldCountries); ?>
    </p>
    <p><label>{%City%}:</label><input type="text" name="form-quote-city" id="form-quote-city"></p>
    <p><label>{%State%}:</label>
    <select id="form-quote-state" name="form-quote-state">
        <option value="" />
    </select>
    </p>
    <p><label>{%Zip%}:</label><input type="text" name="form-quote-zip" id="form-quote-zip"></p>
    <p><label>{%Comments%}:</label><textarea name="form-quote-comments" id="form-quote-comments" ></textarea></p>
    <p><input type="submit" class="formsubmit" name="form-quote-quoteme" id="form-quote-quoteme" value="{%Quote Now!%}"></p>
</form>